@extends('admin.layouts.app')

@section('content')
<div class="row">
    <div class="col-sm-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>{{ $title }}</h5>
            </div>
            <div class="ibox-content">
                <form class="form-inline" id="searchForm" method="get">
                    <input type="hidden" name="store_id" value="{{ $params['store_id'] ?? '' }}">
                    <input type="hidden" name="type" value="{{ $params['type'] ?? '' }}">
                    <div class="form-group">
                        <label>商品名称：</label>
                        <input type="text" name="goods_name" class="form-control" value="{{ $params['goods_name'] ?? '' }}">
                    </div>
                    <div class="form-group">
                        <label>商品编号：</label>
                        <input type="text" name="goods_sn" class="form-control" value="{{ $params['goods_sn'] ?? '' }}">
                    </div>
                    <div class="form-group">
                        <label>权限状态：</label>
                        <select name="has_permission" class="form-control">
                            <option value="">全部</option>
                            <option value="1" {{ isset($params['has_permission']) && $params['has_permission'] == '1' ? 'selected' : '' }}>已授权</option>
                            <option value="0" {{ isset($params['has_permission']) && $params['has_permission'] == '0' ? 'selected' : '' }}>未授权</option>
                        </select>
                    </div>
                    <button type="submit" class="btn btn-primary">搜索</button>
                    <button type="button" class="btn btn-success" onclick="batchGrantPermission('page')">批量授权</button>
                    <button type="button" class="btn btn-success" onclick="batchGrantPermission('all')">全部授权</button>
                    <button type="button" class="btn btn-danger" onclick="batchRevokePermission('page')">批量取消</button>
                    <button type="button" class="btn btn-danger" onclick="batchRevokePermission('all')">全部取消</button>
                </form>

                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th><input type="checkbox" class="check-all"></th>
                                <th>商品ID</th>
                                <th>商品名称</th>
                                <th>商品编号</th>
                                <th>权限状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($data as $item)
                            <tr>
                                <td><input type="checkbox" class="check-item" value="{{ $item->goods_id }}"></td>
                                <td>{{ $item->goods_id }}</td>
                                <td>{{ $item->goods_name }}</td>
                                <td>{{ $item->goods_sn }}</td>
                                <td>
                                    <span class="label {{ $item->has_permission ? 'label-primary' : 'label-default' }}">
                                        {{ $item->has_permission ? '已授权' : '未授权' }}
                                    </span>
                                </td>
                                <td>
                                    <button type="button" class="btn btn-xs {{ $item->has_permission ? 'btn-danger' : 'btn-success' }}"
                                            onclick="updatePermission({{ $item->goods_id }}, {{ $item->has_permission ? 0 : 1 }})">
                                        {{ $item->has_permission ? '取消授权' : '授予权限' }}
                                    </button>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>

                <div class="row">
                    <div class="col-sm-12 text-right">
                        {{ $data->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    $(function() {
        // 全选/取消全选
        $('.check-all').change(function() {
            $('.check-item').prop('checked', $(this).prop('checked'));
        });
    });

    // 更新单个商品权限
    function updatePermission(goodsId, status) {
        $.ajax({
            url: '{{ route("admin.store.sale.strategy.permissions.update") }}',
            type: 'POST',
            data: {
                store_id: '{{ $params["store_id"] }}',
                goods_id: goodsId,
                status: status,
                type: '{{ $params["type"] }}',
                _token: '{{ csrf_token() }}'
            },
            success: function(res) {
                if (res.code === 1) {
                    toastr.success(res.msg);
                    setTimeout(function() {
                        location.reload();
                    }, 1000);
                } else {
                    toastr.error(res.msg);
                }
            }
        });
    }

    // 批量授予权限
    function batchGrantPermission(type) {
        let goodsIds = [];
        if (type === 'page') {
            $('.check-item:checked').each(function() {
                goodsIds.push($(this).val());
            });
            if (goodsIds.length === 0) {
                toastr.warning('请选择要授权的商品');
                return;
            }
        }

        $.ajax({
            url: '{{ route("admin.store.sale.strategy.permissions.batch.grant") }}',
            type: 'POST',
            data: {
                store_id: '{{ $params["store_id"] }}',
                type: type,
                goods_ids: goodsIds,
                strategy_type: '{{ $params["type"] }}',
                _token: '{{ csrf_token() }}'
            },
            success: function(res) {
                if (res.code === 1) {
                    toastr.success(res.msg + '，共授权' + res.data.granted_count + '个商品');
                    setTimeout(function() {
                        location.reload();
                    }, 1000);
                } else {
                    toastr.error(res.msg);
                }
            }
        });
    }

    // 批量取消权限
    function batchRevokePermission(type) {
        let goodsIds = [];
        if (type === 'page') {
            $('.check-item:checked').each(function() {
                goodsIds.push($(this).val());
            });
            if (goodsIds.length === 0) {
                toastr.warning('请选择要取消授权的商品');
                return;
            }
        }

        $.ajax({
            url: '{{ route("admin.store.sale.strategy.permissions.batch.revoke") }}',
            type: 'POST',
            data: {
                store_id: '{{ $params["store_id"] }}',
                type: type,
                goods_ids: goodsIds,
                strategy_type: '{{ $params["type"] }}',
                _token: '{{ csrf_token() }}'
            },
            success: function(res) {
                if (res.code === 1) {
                    toastr.success(res.msg + '，共取消' + res.data.revoked_count + '个商品的权限');
                    setTimeout(function() {
                        location.reload();
                    }, 1000);
                } else {
                    toastr.error(res.msg);
                }
            }
        });
    }
</script>
@endsection 